package com.pipi.fanglan;

import com.pipi.fanglan.cookie.CookieRepository;
import com.pipi.fanglan.cookie.InMemoryCookieRepository;
import com.pipi.fanglan.model.MyCookieStore;
import com.pipi.fanglan.ui.message.repository.InMemoryMessageRepository;
import com.pipi.fanglan.ui.message.repository.Message;
import com.pipi.fanglan.ui.message.repository.MessageRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableCaching
public class FaultMonitorApplication {

	@Bean
	public MessageRepository messageRepository() {
		return new InMemoryMessageRepository();
	}

	@Bean
	public Converter<String, Message> messageConverter() {
		return new Converter<String, Message>() {
			@Override
			public Message convert(String id) {
				return messageRepository().findMessage(Long.valueOf(id));
			}
		};
	}

	@Bean
	public CookieRepository cookieRepository() {
		return new InMemoryCookieRepository();
	}

	@Bean
	public Converter<String, MyCookieStore> cookieConverter() {
		return new Converter<String, MyCookieStore>() {
			@Override
			public MyCookieStore convert(String userName) {
				return cookieRepository().findMyCookieStore(userName);
			}
		};
	}


	public static void main(String[] args) {
		SpringApplication.run(FaultMonitorApplication.class, args);
	}
}
